//
//  OperationViewController.m
//  ReviewDemo
//
//  Created by 龙 on 2018/5/16.
//  Copyright © 2018年 long. All rights reserved.
//

#import "OperationViewController.h"

@interface OperationViewController ()

{
    NSOperationQueue *_threadQueue;
}

@end

@implementation OperationViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _threadQueue = [[NSOperationQueue alloc] init];
    _threadQueue.maxConcurrentOperationCount = 6;
    _threadQueue.name = @"本地数据队列";

    [self startMuchThread];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)startMuchThread
{
//    NSBlockOperation *blockThread = [NSBlockOperation blockOperationWithBlock:^{
//        for (int i = 0; i < 100; i ++)
//        {
//            NSLog(@"第%d个线程 : %@",i,[NSThread currentThread]);
//            [self getPinYin];
//        }
//    }];
//    [_threadQueue addOperation:blockThread];
    
    [_threadQueue addOperationWithBlock:^{
        [self getPinYin];
//        NSLog(@"开了多少个线程------- %@",_threadQueue.currentQueue);

    }];
    
    NSLog(@"开了多少个线程------- %@",_threadQueue);
}

//耗时任务
- (void)getPinYin
{
    NSString *hanziText = @"中国四川";
    
    for (int i = 0 ; i < 10000; i ++)
    {
        // 判断中文长度
        if (hanziText.length > 0)
        {
            // 将中文字符串转成可变字符串
            NSMutableString *pinyinText = [[NSMutableString alloc] initWithString:hanziText];
            
            // 先转换为带声调的拼音
            CFStringTransform((__bridge CFMutableStringRef)pinyinText, 0, kCFStringTransformMandarinLatin, NO);
            NSLog(@"pinyin: %@", pinyinText); // 输出 pinyin: zhōng guó sì chuān
            
            // 再转换为不带声调的拼音
            CFStringTransform((__bridge CFMutableStringRef)pinyinText, 0, kCFStringTransformStripDiacritics, NO);
            NSLog(@"pinyin: %@", pinyinText); // 输出 pinyin: zhong guo si chuan
            
            
            // 转换为首字母大写拼音
            NSString *capitalPinyin = [pinyinText capitalizedString];
            NSLog(@"capitalPinyin: %@", capitalPinyin); // 输出 capitalPinyin: Zhong Guo Si Chuan
            
            // 截取首字母
            NSLog(@"the first letter is '%@'.", [capitalPinyin substringToIndex:1]); // 输出 the first letter is 'Z'.
        }
        
    }
    
}


@end
